package com.Interface.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Interface.dao.SurverInfoMapper;
import com.Interface.dao.SurverUserInfoMapper;
import com.Interface.dao.TopicInfoMapper;
import com.Interface.entity.ActivityUserInfo;
import com.Interface.entity.SurverUserInfo;

@Service
public class SurverInfoService {

	@Autowired
	SurverInfoMapper surverInfoDao;
	
	@Autowired
	SurverUserInfoMapper surverUserInfoMapper;
	
	@Autowired
	TopicInfoMapper topicDao;
	
	public List<Map<String, Object>> selectByTopicId(int topicId){
		return surverInfoDao.selectByTopicId(topicId);
	}
	
	public  int userIsJoin(Map<String, Object> param){
		 return surverUserInfoMapper.userIsJoin(param);
	}
	
	public String userSurver(Map<String, Object> param){
		String rltdesc ="";
		int rlt = userIsJoin(param);
		if(rlt>0){
			rltdesc = "您已经参加了该调研";
		}else{
			String surver = param.get("surver").toString();
			String[] array = surver.split(",");
			for (String str : array){
				SurverUserInfo info = new SurverUserInfo();
				info.setCreateTime(new Date());	
				//info.setSurverQuesNo(Integer.parseInt(answerArray[0]));
				info.setSurverQuesNoRlt(str);
				info.setTopicId( Integer.parseInt(param.get("topic_id").toString()));
				info.setUserId(Integer.parseInt(param.get("user_id").toString()));
				surverUserInfoMapper.insertSelective(info);
			}
			/*
			String surver = param.get("surver").toString();
			String[] array = surver.split(";");
			//int number = 1;
			for (String str : array){
				String[] answerArray = str.split(":");
				String[] qestionArray = answerArray[1].split(",");
				for (String option : qestionArray){
					SurverUserInfo info = new SurverUserInfo();
					info.setCreateTime(new Date());	
					info.setSurverQuesNo(Integer.parseInt(answerArray[0]));
					info.setSurverQuesNoRlt(option);
					info.setTopicId( Integer.parseInt(param.get("topic_id").toString()));
					info.setUserId(Integer.parseInt(param.get("user_id").toString()));
					surverUserInfoMapper.insertSelective(info);
				}
				//number ++;
			}
			*/
			topicDao.updateJoinCnt(Integer.parseInt(param.get("topic_id").toString()));
		}
		return rltdesc;
	}
}
